﻿using System;
using System.Web.Mvc;
using System.Web.Routing;

using System.Web;
using Infrastructure.Crosscutting.NetFramework.Result;

namespace Infrastructure.Crosscutting.NetFramework.Controllers
{
    public abstract class OpenBaseController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            this.AddActionExcutingLog(filterContext);
        }

        protected virtual void AddActionExcutingLog(ActionExecutingContext filterContext)
        {
            var logType = 1;
            var userId = 0;
            var currentUser = filterContext.HttpContext.Session[Infrastructure.Crosscutting.Comm
                .Service.ConstService.SessionNameCurrentUser] as Domain.Entity.User;
            if (currentUser != null)
            {
                userId = currentUser.Id;
            }
            var pageUrl = filterContext.HttpContext.Request.Url == null ? string.Empty : filterContext.HttpContext.Request.Url.ToString();
            pageUrl = pageUrl.Length > 500 ? pageUrl.Substring(0, 500) : pageUrl;

            var refUrl = filterContext.HttpContext.Request.UrlReferrer == null ? string.Empty : filterContext.HttpContext.Request.UrlReferrer.ToString();
            refUrl = refUrl.Length > 500 ? refUrl.Substring(0, 500) : refUrl;

            var shortMessage = "1.running controller " + filterContext.Controller.ToString();
            shortMessage += " , 2.running method " + filterContext.ActionDescriptor.ActionName;
            var fullMessage = "1.browser " + filterContext.HttpContext.Request.Browser.Type;
            fullMessage += " , 2.http method " + filterContext.HttpContext.Request.HttpMethod;
            fullMessage += " , 3.total bytes " + filterContext.HttpContext.Request.TotalBytes.ToString();
            fullMessage += " , 4.user host name " + filterContext.HttpContext.Request.UserHostName;
            fullMessage += " , 5.user agent " + filterContext.HttpContext.Request.UserAgent;
            fullMessage += " , 6.user host address " + filterContext.HttpContext.Request.UserHostAddress;
            fullMessage += " , 7.cookies ";
            for (int i = 0; i < filterContext.HttpContext.Request.Cookies.Count; i++)
            {
                var logCookie = filterContext.HttpContext.Request.Cookies.Get(i);
                fullMessage += " cookie name: " + logCookie.Name;
                fullMessage += "cookie value: " + logCookie.Value;
            }
            var ipAddress = Crosscutting.Comm.Service.StringService.GetRealIP();
            Infrastructure.Data.Uow.Log.LoggerRepositoryManager.AddLogInfo(logType, userId, pageUrl, refUrl, shortMessage, fullMessage, ipAddress);
        }

        protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
        {
            return new CustomJsonResult
            {
                Data = data,
                ContentType = contentType,
                ContentEncoding = contentEncoding,
                JsonRequestBehavior = behavior
            };
        }

        protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding)
        {
            return new CustomJsonResult
            {
                Data = data,
                ContentType = contentType,
                ContentEncoding = contentEncoding
            };
        }
    }
}
